// pages/cet-sound/cet-sound.js
const player = wx.createInnerAudioContext();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    float: "left",
    margin_left: 0,
    play: true,
    soundRes: null,
    answer:""
  },
  listenPlayBtn: function (url) {
    var that = this;
    player.autoplay = true;
    player.src = url.currentTarget.dataset.url;
    player.play();
    this.setData({play: false});
    
  },
  listenParseBtn: function () {
    player.pause();
    this.setData({play: true});
  },
  audioStop: function() {
    player.stop();
  },
  listenLastBtn: function() {
    player.seek(player.currentTime - 1.5);
  },
  listenNextBtn: function() {
    player.seek(player.currentTime + 1.5);
  },

  answerRadioBtn: function(e) {
    this.setData({answer: e.detail.value});
  },

  submitBtn: function(e) {
    var standAnswer = this.data.soundRes.content.answer;
    var userAnswer = this.data.answer;
    if(standAnswer === userAnswer) {
      wx.showToast({
        title: '恭喜你选择正确',
        icon: 'none'
      });
    } else {
      wx.showToast({
        title: '选择错误，继续努力',
        icon: "none"
      });
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.getOpenerEventChannel().on('sound',(res)=>{
      that.setData({soundRes: res});
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.audioCtx = wx.createAudioContext('myAudio')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getOpenerEventChannel().on('sound',(e) => {console.log('onshow ',e)});
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    player.stop();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})